package ch.fhnw.qualiscan.application.views;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.controllers.FillOut;
import ch.fhnw.qualiscan.application.elements.BestPracticeForm;
import ch.fhnw.qualiscan.application.elements.QuestionForm;
import ch.fhnw.qualiscan.application.elements.QuestionHeader;
import ch.fhnw.qualiscan.core.View;
import ch.fhnw.qualiscan.core.db.pojos.Answer;
import ch.fhnw.qualiscan.core.db.pojos.BestPractice;
import ch.fhnw.qualiscan.core.db.pojos.Confidence;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class FillOutView extends View {
	
	private FillOut controller;
	
	
	public FillOutView(FillOut controller) {
		this.controller = controller;
	}

	private Panel panel1;
	private Panel panel2;
	private Panel panel3;
	
	@Override
	public void initComponents() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buildLayout() {
		if (controller.surveyUserRole != null) {
			String domainName = controller.domainModel.getDomainText(controller.currentDomain, controller.userSession.getLanguage()).getText();
			String surveyTitle = controller.currentSurvey.getTitle();
		
			VerticalLayout fillOutWrapper = new VerticalLayout();
			fillOutWrapper.setSpacing(true);
			fillOutWrapper.setMargin(false, false, true, false);
			
			fillOutWrapper.addComponent(textHelper.h1(TM.get("fillout.title", domainName)));
			fillOutWrapper.addComponent(textHelper.p(TM.get("fillout.desc", surveyTitle)));
			
			addComponent(fillOutWrapper);
			
			addComponent(buildPanel1());
			
			
			// slider
			addComponent(buildPanel2());
	
			// best practice
			addComponent(buildPanel3());
			
			//Save Button
			Button saveButton = new Button(TM.get("fillout.save"), new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					
					event.getButton().setEnabled(false);
					event.getButton().setCaption(TM.get("action.saving"));
					
					controller.save();
					
				}
			});
			saveButton.setIcon(Config.ICON_EDIT_APP);
			
			HorizontalLayout button = new HorizontalLayout();
			button.addComponent(saveButton);
			button.setMargin(true, true, true, false);
			this.addComponent(button);
		}
		
	}
	
	private Panel buildPanel1() {
		panel1 = new Panel();
		panel1.addComponent(textHelper.h2(TM.get("fillout.questions.title")));
		
		// header
		HorizontalLayout header = new HorizontalLayout();
		header.setWidth("100%");
		
		HorizontalLayout headerForm = new HorizontalLayout();
		
		// first column (agree)
		Label c1 = new Label(TM.get("answer.1"));
		c1.setStyleName("answer1");
		c1.setHeight("100%");
		
		// second column 
		Label c2 = new Label(TM.get("answer.2"));
		c2.setStyleName("answer2");
		c2.setHeight("100%");
		
		// second column 
		Label c3 = new Label(TM.get("answer.3"));
		c3.setStyleName("answer3");
		c3.setHeight("100%");
		
		// second column 
		Label c4 = new Label(TM.get("answer.4"));
		c4.setStyleName("answer4");
		c4.setHeight("100%");
		
		// second column 
		Label c5 = new Label(TM.get("answer.5"));
		c5.setStyleName("answer5");
		c5.setHeight("100%");

		headerForm.addComponent(c1);
		headerForm.addComponent(c2);
		headerForm.addComponent(c3);
		headerForm.addComponent(c4);
		headerForm.addComponent(c5);
		headerForm.setWidth("290px");
		headerForm.setHeight("35px");
		headerForm.setStyleName("formColumns");
		
		Label headerSpacer = new Label("");
		header.addComponent(headerSpacer);
		header.addComponent(headerForm);
		header.setExpandRatio(headerSpacer, 1.0f);
		
		panel1.addComponent(header);
	
		Iterator questionIterator = controller.currentDomain.getQuestions().iterator();

		int counter = 0;
		int subCounter = 0;
		while (questionIterator.hasNext()) {
			subCounter = 0;
			counter++;
			Question currentQuestion = (Question) questionIterator.next();
			if(currentQuestion.getSubquestions().size() > 0) {
				//Question has subquestions
				panel1.addComponent(new QuestionHeader(counter, controller.questionModel.getQuestionText(controller.userSession.getLanguage(),currentQuestion).getText(), false));
				Iterator subQuestionIterator = currentQuestion.getSubquestions().iterator();
				while(subQuestionIterator.hasNext()) {
					subCounter++;
					Question currentSubQuestion = (Question) subQuestionIterator.next();
					QuestionForm qf = new QuestionForm(controller.questionModel.getAnswer(controller.surveyUserRole, currentSubQuestion),currentSubQuestion, counter, subCounter, controller.questionModel.getQuestionText(controller.userSession.getLanguage(),currentSubQuestion).getText(), true);
					controller.questionFormList.add(qf);
					panel1.addComponent(qf);
				}
			}
			else {
				//Question has no subquestion
				QuestionForm qf = new QuestionForm(controller.questionModel.getAnswer(controller.surveyUserRole, currentQuestion),currentQuestion,counter, -1, controller.questionModel.getQuestionText(controller.userSession.getLanguage(),currentQuestion).getText(), false);
				controller.questionFormList.add(qf);
				panel1.addComponent(qf);
			}
		}
		return panel1;
	}
	
	private Panel buildPanel2() {
		
		panel2 = new Panel();
		
		Label sliderTitle = textHelper.h2(TM.get("fillout.slider.title"));
		Label sliderDesc = textHelper.p(TM.get("fillout.slider.desc"));
		
		List<Integer> answers = Arrays.asList(new Integer[] {1,2,3});
		
		OptionGroup confidenceOg = new OptionGroup(null, answers);
		confidenceOg.setItemCaption(1, TM.get("fillout.confidence.0"));
		confidenceOg.setItemCaption(2, TM.get("fillout.confidence.1"));
		confidenceOg.setItemCaption(3, TM.get("fillout.confidence.2"));
		confidenceOg.setStyleName("h-optiongroup confidenceCheckboxes");
		
		Confidence confidence = controller.domainModel.getConfidenceOfDomain(controller.surveyUserRole, controller.currentDomain);
		if (confidence != null) {
			confidenceOg.setValue(confidence.getConfidence());
		}
		else {
			confidenceOg.setValue(2);
		}
		confidenceOg.addListener(new Property.ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				controller.newConfidenceValue = (Integer) (event.getProperty().getValue());
			}
		});
		
		confidenceOg.setWidth("260px");
		
		HorizontalLayout sliderWrapper = new HorizontalLayout();
		sliderWrapper.addComponent(sliderDesc);
		sliderWrapper.addComponent(confidenceOg);
		sliderWrapper.setSpacing(true);
		sliderWrapper.setMargin(true);
		sliderWrapper.setWidth("100%");
		sliderWrapper.setExpandRatio(sliderDesc, 1.0f);
		
		panel2.addComponent(sliderTitle);
		panel2.addComponent(sliderWrapper);
		
		return panel2;
	}
	
	private Panel buildPanel3() {
		panel3 = new Panel();
		
		controller.bpf = new BestPracticeForm();
		BestPractice bp = controller.domainModel.getBestPracticeOfDomain(controller.surveyUserRole, controller.currentDomain);
		if (bp != null) {
			controller.bpf.setValue(bp.getBestpractice(), bp.getTitle(), bp.getPublicpost());
		}
		panel3.addComponent(controller.bpf);
		
		
		
		return panel3;
	}

	@Override
	public void reBuild() {
		// TODO Auto-generated method stub
		
	}

}
